package com.blue.common.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.blue.common.threadlocal.PageParamsThreadLocal;
import com.blue.common.utils.DataUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 基础Service<br>
 * Service实现类可以继承该基础类，该类封装了CRUD操作<br>
 * 该Service基础类不是必须的
 * 
 * @param <T>
 *            Mapper对应的实体对象
 * @param <PK>
 *            主键的类型
 * @author 杨浩泉
 * @version 2016年10月14日
 */
public class BaseService<T, PK> implements IService<T, PK> {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BaseMapper<T, PK> baseMapper;

	@Override
	public int insert(T entity) {
		return this.baseMapper.insert(entity);
	}

	@Override
	public int insertSelective(T entity) {
		return this.baseMapper.insertSelective(entity);
	}

	@Override
	public int insertBatch(List<T> entityList) {
		return this.baseMapper.insertList(entityList);
	}

	@Override
	public int inserOrUpdate(T entity) {
		return this.baseMapper.inserOrUpdate(entity);
	}

	@Override
	public int delete(PK id) {
		return this.baseMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteSelective(T entity) {
		return this.baseMapper.delete(entity);
	}

	@Override
	public int deleteBatchIds(List<PK> idList) {
		return this.baseMapper.deleteBatchIds(idList);
	}

	@Override
	public int update(T entity) {
		return this.baseMapper.updateByPrimaryKey(entity);
	}

	@Override
	public int updateSelective(T entity) {
		return this.baseMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int update(T entity, T whereEntity) {
		return this.baseMapper.update(entity, whereEntity);
	}

	@Override
	public int updateSelective(T entity, T whereEntity) {
		return this.baseMapper.updateSelective(entity, whereEntity);
	}

	@Override
	public int updateBatchById(List<T> entityList) {
		return this.baseMapper.updateBatchById(entityList);
	}

	@Override
	public T selectById(PK id) {
		return this.baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<T> selectBatchIds(List<PK> idList) {
		return this.baseMapper.selectBatchIds(idList);
	}

	@Override
	public T selectOne(T entity) {
		return this.baseMapper.selectOne(entity);
	}

	@Override
	public int selectCount(T entity) {
		return this.baseMapper.selectCount(entity);
	}

	@Override
	public List<T> selectList(T entity, String orderByField) {
		return this.baseMapper.selectList(entity, orderByField);
	}

	@Override
	public List<T> selectList(T entity) {
		return this.baseMapper.select(entity);
	}

	@Override
	public PageInfo<T> selectPage(T entity, String orderByField) {
		Page<T> page = PageHelper.startPage(PageParamsThreadLocal.getPageNum(), PageParamsThreadLocal.getPageSize(), orderByField);
		page.clear();
		this.baseMapper.selectPage(entity);
		return new PageInfo<T>(page);
	}

	@Override
	public PageInfo<T> selectPage(T entity) {
		return selectPage(entity, null);
	}

	@Override
	public PageInfo<T> selectPageByMap(Map<String, Object> params) {
		Map<String, Object> pageMap = new HashMap<String, Object>();
		if (DataUtil.isEmpty(params.get("pageNum"))) {
			pageMap.put("pageNum", 1);
		}
		if (DataUtil.isEmpty(params.get("pageSize"))) {
			pageMap.put("pageSize", 10);
		}
		if (DataUtil.isEmpty(params.get("orderBy"))) {
			pageMap.put("orderBy", "id desc");
		}

		Page<T> page = PageHelper.startPage(pageMap);
		baseMapper.selectPageByMap(params);
		return new PageInfo<T>(page);
	}

}
